package com.Policing.Receive;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.Policing.Adapter.Reception_Adapter;
import com.Policing.Ip_set.Ip_Set;
import com.Policing.Listview_animation.BounceLoadingView;
import com.Policing.R;
import com.Policing.bean.Reception_bean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/1/21.
 */

public class Receive_reception extends Activity {
    private BounceLoadingView loadingView;
    private ListView listView;
    private String url,urlup;
    private String receivesId;
    private String assetId;
    private String get;
    private List<String> id = new ArrayList<>();
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.receive_reception);
        url = "http://"+ Ip_Set.IP+"/api/receivesAssetQuery";
        urlup = "http://"+ Ip_Set.IP+"/api/receivesGet";
        inview();
        load();

    }

    private void load() {
        new Thread(){
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("receivesId",receivesId)
                            .build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(requestBody).build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.i("222",responseData);
                    showResponse(responseData);//解析
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }.start();
    }

    private void showResponse(final String responseData) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    list.clear();
                    id.clear();
                    loadingView.stop();
                    loadingView.setVisibility(View.GONE);
                    Gson gson = new Gson();
                    List<Reception_bean> userList = gson.fromJson(responseData, new TypeToken<List<Reception_bean>>(){}.getType());
                    for(int i = 0;i < userList.size();i++){
                        Map<String, Object> map = new HashMap<String, Object>();
                           map.put("assetName",userList.get(i).getAssetName());
                           map.put("assetNumber",userList.get(i).getAssetNumber());
                            id.add(userList.get(i).getAssetId());
                        list.add(map);
                    }
                    Reception_Adapter receive_adapter = new Reception_Adapter(Receive_reception.this,list);
                    listView.setAdapter(receive_adapter);

                }
            });
    }

    private void inview() {
        loadingView = (BounceLoadingView) findViewById(R.id.loadingView);
        loadingView.addBitmap(R.mipmap.v4);
        loadingView.addBitmap(R.mipmap.v5);
        loadingView.addBitmap(R.mipmap.v6);
        loadingView.addBitmap(R.mipmap.v7);
        loadingView.addBitmap(R.mipmap.v8);
        loadingView.addBitmap(R.mipmap.v9);
        loadingView.setShadowColor(Color.LTGRAY);
        loadingView.setDuration(700);
        loadingView.start();
        Intent in = getIntent();
        receivesId = in.getExtras().getString("receivesId");
        //---------------------
        listView = (ListView) findViewById(R.id.reception_lv);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int in, long q) {
                new AlertView("是否接收！", null, "取消", null,
                        new String[]{"接收", "退回"},
                        Receive_reception.this, AlertView.Style.ActionSheet, new OnItemClickListener(){
                    public void onItemClick(Object o,int position){
                        if (position==1){
                            //退回
                            assetId = id.get(in);
                            get="2";
                            upload();
                        }
                        if (position==0){
                            assetId = id.get(in);
                            get="1";
                            upload();
                            //接收
                        }
                        //Toast.makeText(Receive_reception.this, "点击了第" + position + "个",
                               // Toast.LENGTH_SHORT).show();
                    }
                }).show();
            }
        });
    }

    private void upload() {
        new Thread(){
            @Override
            public void run() {
                try {
                    Log.i("123123",receivesId+assetId+get);
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("receivesId",receivesId)
                            .add("assetId",assetId)
                            .add("get",get)
                            .build();
                    Request request = new Request.Builder()
                            .url(urlup)
                            .post(requestBody).build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.i("222",responseData);
                    showResponse1(responseData);//解析
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }.start();
    }

    private void showResponse1(final String responseData) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    JSONObject js = new JSONObject(responseData);
                    if (js.getString("state").equals("1")){
                             load();
                            Toast.makeText(Receive_reception.this,"提交成功",Toast.LENGTH_SHORT).show();
                    }
                    if (js.getString("state").equals("0")){

                        Toast.makeText(Receive_reception.this,"提交失败",Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
